package com.lpit.service.impl;

import com.lpit.constant.Constant;
import com.lpit.dto.CommentsDto;
import com.lpit.mapper.CommentMapper;
import com.lpit.result.ResponseResult;
import com.lpit.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author Yao Chen
 */
@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentMapper commentMapper;
    /**
     * 添加评论
     * @param commentsDto
     * @author Yao Chen
     * @return ResponseResult<Integer>
     */
    @Override
    public ResponseResult<Integer> addComments(CommentsDto commentsDto) {
        if (null == commentsDto.getCommentTime() || "".equals(commentsDto.getCommentTime())) {
            commentsDto.setCommentTime(new SimpleDateFormat(Constant.TIME_FORMAT).format(new Date()));
        }
        if (null == commentsDto.getUserId() || 0 == commentsDto.getUserId()) {
            return new ResponseResult<>(404, null, Constant.THE_USER_WAS_NOT_FOUND);
        }
        if (null == commentsDto.getArticleId() || 0 == commentsDto.getArticleId()) {
            return new ResponseResult<>(404, null, Constant.NO_RELATED_ARTICLE_FOUND);
        }
        Integer integer = commentMapper.addComments(commentsDto);
        if(null == integer || 0 == integer){
            return new ResponseResult<>(404, null, Constant.MISTAKE);
        }
        return new ResponseResult<>(200, integer, Constant.SUCCESS);
    }

    /**
     * 累计评论数
     */
    @Override
    public ResponseResult<Long> getCommentCount() {
        Long accumulated = commentMapper.accumulated();
        return new ResponseResult<>(accumulated);
    }
}
